//
//  decisionPoint.m
//  decision
//
//  Created by Zhiguo on 03.02.10.
//  Copyright 2010 Home. All rights reserved.
//

#import "decisionPoint.h"

static sqlite3_stmt *init_statement = nil;
static sqlite3_stmt *insert_statement = nil;

@implementation decisionPoint
@synthesize pId, truncate, desc, variable, weight, decisionId;

-(id)initWithPid:(NSInteger)paraPid database:(sqlite3 *)db {

	if(self = [super init]) {
		pId = paraPid;
		database = db;
		if(init_statement == nil){
			const char *sql = "select truncate from decision where id = ?";
			if(sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK){
				NSAssert1(0, @"error, failed to prepare statement with message '%s'", sqlite3_errmsg(database));
			
			}
				
		}
		
		sqlite3_bind_int(init_statement, 1, pId);
		if(sqlite3_step(init_statement) == SQLITE_ROW){
			
			self.truncate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];
			//NSLog(@"truncate: %@", self.truncate);
			
		}else {
			self.truncate = @"There is no decision now";
		}
		
		sqlite3_reset(init_statement);
	}
	return self;
}

-(id)initWithTandD:(NSString *)truncatePara desc:(NSString *)descPara {
	
	self.truncate = truncatePara;
	self.desc = descPara;
	return self;
}

-(NSInteger)insertNewDpIntoDatabase {
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentDirectory = [paths objectAtIndex:0];
	NSString *path = [documentDirectory stringByAppendingPathComponent:@"goodP.sqlite"];
	
	if(sqlite3_open([path UTF8String], &database) == SQLITE_OK){
		if(insert_statement == nil){
				const char *sql = "INSERT INTO decision(id, goodP, badP, truncate, description) values(null, 0, 0, ?, ?)";
				if(sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK){
					NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
				}
		}
		
		//NSLog(@"self.truncate :%@",self.truncate);
		
			//truncate must be assigned
			if(self.truncate == nil){
				return -1;
			}
			
			if(self.desc == nil){
				sqlite3_bind_text(insert_statement, 1, [self.desc UTF8String], -1, SQLITE_TRANSIENT);
				
			}else{
				sqlite3_bind_text(insert_statement, 2, [self.desc UTF8String], -1, SQLITE_TRANSIENT);
				sqlite3_bind_text(insert_statement, 1, [self.truncate UTF8String], -1, SQLITE_TRANSIENT);
			}
			
			int success = sqlite3_step(insert_statement);
			//NSLog(@"success :%d",success);
			insert_statement = nil;
			if(success != SQLITE_ERROR){
				
				return sqlite3_last_insert_rowid(database);
				
			}
			
			NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
			return -1;	
			
		sqlite3_finalize(insert_statement);
	}else {
		NSLog(@"database open has problem %@", sqlite3_errmsg(database));
	}
	
	return 1;
	
}

//+(NSInteger)insertNewDpIntoDatabase:(decisionPoint *)newDp  {
//	database = db;
//	if(insert_statement == nil){
//		
//		//const char *sql = "INSERT INTO decision(goodP, badP, truncate, descriptopm) values(0, 0, 'test', 'test')";
//		const char *sql = "select * from decision";
//		if(sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK){
//			NSLog(@"I am here");
//			NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
//		}
//	}
//	
//	if(newdptruncate == nil) {
//		return -1;
//	}
//	
	//if(newdpdesc == nil){
//		sqlite3_bind_text(insert_statement, 1, [newdptruncate UTF8String], -1, SQLITE_TRANSIENT);
//		
//	}else{
//		sqlite3_bind_text(insert_statement, 2, [newdpdesc UTF8String], -1, SQLITE_TRANSIENT);
//		sqlite3_bind_text(insert_statement, 1, [newdptruncate UTF8String], -1, SQLITE_TRANSIENT);
//	}
//	
//	int success = sqlite3_step(insert_statement);
//	
//	if(success != SQLITE_ERROR){
//		return sqlite3_last_insert_rowid(database);
//		
//	}
//	
//	NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
//	return -1;
//	
//}

-(void)deleteDpFromDatabase{
}
@end
